package com.lbh.cloudgallery.service.impl;

import com.lbh.cloudgallery.entity.ProductEntity;
import com.lbh.cloudgallery.entity.vo.StarRespVo;
import com.lbh.cloudgallery.entity.vo.StarVo;
import com.lbh.cloudgallery.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.CollectionDao;
import com.lbh.cloudgallery.entity.CollectionEntity;
import com.lbh.cloudgallery.service.CollectionService;


@Service("collectionService")
public class CollectionServiceImpl extends ServiceImpl<CollectionDao, CollectionEntity> implements CollectionService {

    @Autowired
    ProductService productService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CollectionEntity> page = this.page(
                new Query<CollectionEntity>().getPage(params),
                new QueryWrapper<CollectionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public int changeStar(Integer startStaus, StarVo vo) {
        //判断是否收藏了，如果已收藏则取消
        int resp=0;
        if (startStaus==0){
            //未收藏则添加
            CollectionEntity entity = new CollectionEntity();
            BeanUtils.copyProperties(vo,entity);
            baseMapper.insert(entity);
            resp = 1;
        }else {
            baseMapper.delete(new QueryWrapper<CollectionEntity>().eq("member_id", vo.getMemberId()).eq("product_id", vo.getProductId()));
            resp = 0;
        }
        return resp;
    }

    @Override
    public List<StarRespVo> listStar(Long memberId) {
        List<CollectionEntity> list = baseMapper.selectList(new QueryWrapper<CollectionEntity>().eq("member_id", memberId));

        List<StarRespVo> collect = list.stream().map(v -> {
            ProductEntity byId = productService.getById(v.getProductId());
            StarRespVo starRespVo = new StarRespVo();
            starRespVo.setProductId(byId.getId());
            starRespVo.setPrice(byId.getPrice());
            starRespVo.setProductImage(byId.getImage());
            starRespVo.setProductName(byId.getName());
            starRespVo.setSaleCount(byId.getSaleCount());
            return starRespVo;
        }).collect(Collectors.toList());

        return collect;
    }

}